package com.mx.adapt.pager.indicator

import android.content.Context
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.RadioButton
import android.widget.RadioGroup
import com.mx.adapt.utils.MXUtils

abstract class MXBaseIndicator : IMXIndicatorBuild {
    protected var indicatorGroup: RadioGroup? = null
    private var checkedIndex = -1
    override fun attachGroup(parent: FrameLayout) {
        if (indicatorGroup != null) return
        MXUtils.log("attachGroup()")

        val group = RadioGroup(parent.context)
        group.orientation = RadioGroup.HORIZONTAL
        group.isSoundEffectsEnabled = false
        group.isEnabled = false

        val lp = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.WRAP_CONTENT,
            FrameLayout.LayoutParams.WRAP_CONTENT
        )
        val margin = getGroupMargin(parent.context)
        lp.setMargins(margin, margin, margin, margin)
        lp.gravity = Gravity.BOTTOM or Gravity.RIGHT
        parent.addView(group, lp)
        indicatorGroup = group
    }

    override fun detachGroup(parent: FrameLayout) {
        val group = indicatorGroup ?: return
        MXUtils.log("detachGroup()")

        parent.removeView(group)
        indicatorGroup = null
    }

    override fun buildItemSize(size: Int) {
        val group = indicatorGroup ?: return
        val currentSize = group.childCount
        MXUtils.log("buildItemSize($size  ->  $currentSize)")
        if (size > 0 && size == currentSize) {
            group.visibility = View.VISIBLE
            return
        }
        if (size == 0) {
            group.visibility = View.GONE
            return
        }
        group.visibility = View.VISIBLE

        if (size > currentSize) {
            repeat(size - currentSize) {
                addIndicatorItem(group)
            }
        } else {
            repeat(currentSize - size) {
                group.removeView(group.getChildAt(0))
            }
        }
    }

    private fun addIndicatorItem(group: RadioGroup) {
        val size = getNormalItemSize(group.context)
        val margin = getItemMargin(group.context) / 2
        val view = RadioButton(group.context)
        view.buttonDrawable = null
        view.isChecked = false
        view.isSoundEffectsEnabled = false
        view.isEnabled = false
        view.setBackgroundResource(getItemBackgroundRes())
        val lp = RadioGroup.LayoutParams(size[0], size[1])
        lp.leftMargin = margin
        lp.rightMargin = margin
        group.addView(view, lp)
    }

    override fun selectIndicator(maxSize: Int, index: Int) {
        val group = indicatorGroup ?: return
        if (group.childCount != maxSize) {
            buildItemSize(maxSize)
        }
        if (checkedIndex == index) {
            return
        }
        val size = group.childCount
        if (size <= 0) return
        val index = index % size
        val items = (0..group.childCount).mapNotNull {
            group.getChildAt(it) as RadioButton?
        }
        val normal = getNormalItemSize(group.context)
        val select = getSelectItemSize(group.context)
        items.forEachIndexed { i, button ->
            button.isChecked = (i == index)
            val lp = button.layoutParams as RadioGroup.LayoutParams
            val size = if (i == index) select else normal
            lp.width = size[0]
            lp.height = size[1]
            button.layoutParams = lp
        }
        checkedIndex = index
    }

    /**
     * 获取Margin边距
     */
    abstract fun getGroupMargin(context: Context): Int

    /**
     * 获取每个Item的宽高
     */
    abstract fun getNormalItemSize(context: Context): IntArray
    abstract fun getSelectItemSize(context: Context): IntArray

    /**
     * 获取Item的间距
     */
    abstract fun getItemMargin(context: Context): Int

    /**
     * 获取Item的背景图
     */
    abstract fun getItemBackgroundRes(): Int
}